from django.shortcuts import render, HttpResponse, redirect, reverse
from django.http import response
from . import forms


def test(request):
    """
    auth 应用测试接口
    :param request:
    :return:
    """
    return HttpResponse('Test')


def login(request):
    if request.method == 'GET':
        form = forms.UserInfoModelForm()
        context = {
            'form': form
        }
        return render(request, 'login.html', context=context)
    form = forms.UserInfoModelForm(data=request.POST)
    if form.is_valid():
        resp = response.HttpResponseRedirect(redirect_to=reverse('library:booklist'))
        resp.set_cookie('user', 'zhangsan')
        return resp
    return redirect(reverse('myauth:login'))


def logout(request):
    return redirect(reverse('library:booklist'))
